package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.domain.dto.SetmealDto;
import com.example.domain.entity.Category;
import com.example.domain.entity.Setmeal;
import com.example.service.ICategoryService;
import com.example.service.ISetmealDishService;
import com.example.service.ISetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 套餐 前端控制器
 * </p>
 *
 * @author xiaoduan
 * @since 2024-07-30
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private ISetmealService setmealService;

    @Autowired
    private ISetmealDishService setmealDishService;

    @Autowired
    private ICategoryService categoryService;

    @GetMapping("/page")
    public R page(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize, @RequestParam(defaultValue = "") String name) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        wrapper.like(StringUtils.isNotBlank(name),Setmeal::getName,name);
        IPage<Setmeal> setmealIPage = new Page<>(page, pageSize);
        IPage<SetmealDto> setmealDtoIPage = new Page<>();
        setmealService.page(setmealIPage, wrapper);
        BeanUtils.copyProperties(setmealIPage,setmealDtoIPage,"records");
        List<SetmealDto> list = setmealIPage.getRecords().stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            Category one = categoryService.getById(item.getCategoryId());
            if (one != null) {
                setmealDto.setCategoryName(one.getName());
            }
            return setmealDto;
        }).collect(Collectors.toList());
        setmealDtoIPage.setRecords(list);
        return R.success(setmealDtoIPage);
    }

    //新增
    @PostMapping()
    @CacheEvict(value = "setmealCache",allEntries = true)  // 先清除缓存
    public R save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveSetmeal(setmealDto);
        return R.success("新增套can成功");
    }
    //修改
    @GetMapping("{id}")
    @Cacheable(value = "setmealCache",key = "#id",unless = "#result==null")
    public R findById(@PathVariable Long id) {
        return R.success(setmealService.getStemealById(id));
    }
    @PutMapping()
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R update(@RequestBody SetmealDto setmealDto) {
        setmealService.updateSetmeal(setmealDto);
        return R.success("修改套can成功");
    }
    //批量删除
    @DeleteMapping()
    @CacheEvict(value = "setmealCache",allEntries = true)
    public R delete(String ids) {
        setmealService.removeByIdsAndSetMealDish(ids);
        return R.success("删除成功");
    }

    //批量起售、停售
    @PostMapping("/status/{status}")
    public R piliangQishouTingshou(@PathVariable Integer status,String ids) {
        setmealService.piLiangQiShouTingShou(status,ids);
        return R.success("批量起售/停售");
    }


    /**
     * 根据条件查询套餐数据
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId + '_' + #setmeal.status",unless = "#result==null")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);

        return R.success(list);
    }
}

